package com.ttc.mBackup.BackupContacts;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.provider.ContactsContract.CommonDataKinds.Im;
import android.util.Log;



public class JsonParser {

	public JsonParser() {
		super();

	}

	public ArrayList<Contact> fromJSONString(String jString) {
		ArrayList<Contact> contactArraylist = new ArrayList<Contact>();
		try {
			JSONObject jObj = new JSONObject(jString);
			JSONArray contactsArr = jObj.getJSONArray("contacts");
			if (contactsArr != null) {
				int n = contactsArr.length();
				Log.i("SO CONTACTS", "" + n);
				if (n > 0) {
					JSONObject contactElement = null;

					for (int i = 0; i < n; i++) {
						contactElement = (JSONObject) contactsArr.get(i);
						Contact ct = new Contact();

						// StructuredName
						JSONObject jStructuredName = contactElement
								.getJSONObject("structuredName");
						StructuredName structuredName = new StructuredName();
						try {
							structuredName.setDisplayName(jStructuredName
									.getString("displayName"));
						} catch (Exception ex) {
						}
						try {
							structuredName.setFamilyName(jStructuredName
									.getString("familyName"));
						} catch (Exception ex) {
						}

						try {
							structuredName.setGivenName(jStructuredName
									.getString("givenName"));
						} catch (Exception ex) {
						}
						try {
							structuredName.setMiddleName(jStructuredName
									.getString("middleName"));
						} catch (Exception ex) {
						}
						try {
							structuredName.setNamePrefix(jStructuredName
									.getString("namePrefix"));
						} catch (Exception ex) {
						}
						try {
							structuredName.setNameSuffix(jStructuredName
									.getString("nameSuffix"));
						} catch (Exception ex) {
						}
						try {
							structuredName.setPhoneticFamilyName(jStructuredName
									.getString("phoneticFamilyName"));
						} catch (Exception ex) {
						}

						try {
							structuredName.setPhoneticGivenName(jStructuredName
									.getString("phoneticGivenName"));
						} catch (Exception ex) {
						}
						try {
							structuredName.setPhoneticMiddleName(jStructuredName
									.getString("phoneticMiddleName"));
						} catch (Exception ex) {
						}

						ct.setStructuredName(structuredName);

						// //Address
						JSONArray addressesArr = contactElement
								.getJSONArray("addresses");
						ArrayList<Address> addresses = new ArrayList<Address>();
						JSONObject jAddress;
						Log.i("LENGTH address of CONTACT", ""
								+ addressesArr.length());
						if (addressesArr.length() > 0) {
							for (int j = 0; j < addressesArr.length(); j++) {
								jAddress = (JSONObject) addressesArr.get(j);
								Address add = new Address();
								try {
									add.setCity(jAddress.getString("city"));
								} catch (Exception ex) {
								}
								try {
									add.setCountry(jAddress
											.getString("country"));
								} catch (Exception ex) {
								}
								try {
									add.setNeighborhood(jAddress
											.getString("neighborhood"));
								} catch (Exception ex) {
								}
								try {
									add.setCity(jAddress.getString("poBox"));
								} catch (Exception ex) {
								}
								try {
									add.setStreet(jAddress.getString("street"));
								} catch (Exception ex) {
								}
								try {
									add.setType(jAddress.getString("type"));
								} catch (Exception ex) {
								}
								try {
									add.setZipCode(jAddress
											.getString("zipCode"));
								} catch (Exception ex) {
								}

								addresses.add(add);
							}
							ct.setAddresses(addresses);
						}
						// Email
						JSONArray emailsArr = contactElement
								.getJSONArray("emails");
						ArrayList<Email> emails = new ArrayList<Email>();
						JSONObject jEmail;
						Log.i("Emails size", ""
								+ emailsArr.length());
						if (emailsArr.length() > 0) {
							for (int j = 0; j < emailsArr.length(); j++) {
								jEmail = (JSONObject) emailsArr.get(j);
								Email email = new Email();
								try {
									email.setEmail(jEmail.getString("email"));
								} catch (Exception ex) {
								}
								try {
									email.setType(jEmail.getString("type"));
								} catch (Exception ex) {
								}
								emails.add(email);
							}
							ct.setEmails(emails);
						}
						//						
						// //IM
						JSONArray imsArr = contactElement.getJSONArray("ims");
						ArrayList<IM> ims = new ArrayList<IM>();
						JSONObject jIm;
						Log.i("IMs size", ""
								+ imsArr.length());
						if (imsArr.length() > 0) {
							for (int j = 0; j < imsArr.length(); j++) {
								jIm = (JSONObject) imsArr.get(j);
								IM im=new IM();
								try {
									im.setIm(jIm.getString("im"));
								} catch (Exception ex) {
								}
								try {
									im.setType(jIm.getString("type"));
								} catch (Exception ex) {
								}
								ims.add(im);
							}
							
							ct.setIms(ims);
						}
						// Organization
						JSONArray orgArr = contactElement
								.getJSONArray("organizations");
						ArrayList<Organization> orgs = new ArrayList<Organization>();
						JSONObject jOrg;
						if (orgArr.length() > 0) {
							for (int j = 0; j < orgArr.length(); j++) {
								jOrg = (JSONObject) orgArr.get(j);
								Organization org = new Organization();
								try {
									org.setCompany(jOrg.getString("company"));
								} catch (Exception ex) {
								}
								try {
									org.settitle(jOrg.getString("title"));
								} catch (Exception ex) {
								}
								try {
									org.setType(jOrg.getString("type"));
								} catch (Exception ex) {
								}
								orgs.add(org);
							}
							ct.setOrganizations(orgs);
						}
						// //Phone
						JSONArray phoneArr = contactElement
								.getJSONArray("phones");
						ArrayList<Phone> phones = new ArrayList<Phone>();
						JSONObject jPhone;
						if (phoneArr.length() > 0) {
							for (int j = 0; j < phoneArr.length(); j++) {
								jPhone = (JSONObject) phoneArr.get(j);
								Phone phone = new Phone();
								try {
									phone.setNumber(jPhone.getString("number"));
								} catch (Exception ex) {
								}
								try {
									phone.setType(jPhone.getString("type"));
								} catch (Exception ex) {
								}
								phones.add(phone);
							}
							ct.setPhones(phones);
						}
						contactArraylist.add(ct);
						Log.i("SO222 CONTACTS", "" + contactArraylist.size());
					}
				}

			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return contactArraylist;
	}

}
